//
//  ChangeFavCurrencyViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/2/20.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit

class ChangeFavCurrencyViewController: QMUICommonViewController {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var btnSave: QMUIFillButton!
    @IBOutlet weak var btnCancel: QMUIFillButton!
    
    @IBAction func actionClick(_ sender: QMUIButton) {
        if sender.tag == 0 {
            
        }else {
            if moneys.count > 0 {
                let fav =   ["\(currentShop!.id)":moneys]
                UserDefaults.standard.set(fav,forKey: "favMoney")
            }else {
                QMUITips.showInfo(LanguageHelper.getString(key: "请至少选择一种货币"))
            }
        }
        closeClick()
        
    }
    
    var cancelBlock:(()->())?
    
    
    func closeClick() {
        if cancelBlock != nil {
            cancelBlock!()
        }
    }
    
    var isChoose:Bool = true
    var moneys = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()

        // Do any additional setup after loading the view.
    }
    
    func setupView() {
        self.tableView.register(UINib(nibName: "CurrencyViewCell", bundle: nil), forCellReuseIdentifier: "cellItem")
        self.tableView.separatorStyle = .none
        self.tableView.rowHeight = 64
        self.btnSave.setTitle(LanguageHelper.getString(key: "保存"), for: UIControl.State.normal)
        self.btnCancel.setTitle(LanguageHelper.getString(key: "BtnCancel"), for: UIControl.State.normal)

        
        self.titleLabel.text = LanguageHelper.getString(key: "设置收藏货币")
        
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ChangeFavCurrencyViewController:QMUITableViewDelegate,QMUITableViewDataSource,QMUIModalPresentationContentViewControllerProtocol {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return appMoneyItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellItem") as! CurrencyViewCell
        let item:MoneyItem = appMoneyItems[indexPath.row]
        cell.icon.kf.setImage(with: URL(string: IMAGE_HOST + item.image))
        cell.currency.text = item.ex_title
        
        let locale = LanguageHelper.shareInstance.locale
        if locale == "en" {
            cell.label.text = item.etitle
        } else if locale == "th" {
            cell.label.text = item.ttitle
        }else {
            cell.label.text = item.title
        }
        cell.hasIn.isHidden = false
        
        if moneys.count > 0 {
            if let index =  moneys.lastIndex(of: item.ex_title) {
                cell.hasIn.text = "\(index + 1)"
                
                cell.checkView.isHidden =  false
            } else {
                cell.hasIn.text = nil
                cell.checkView.isHidden = true
            }
        }
        
        
        return cell
    }
    
    func preferredContentSize(in controller: QMUIModalPresentationViewController!, keyboardHeight: CGFloat, limitSize: CGSize) -> CGSize {
        let screenWidth = UIScreen.main.bounds.width - 48
        return  CGSize(width: screenWidth, height: 440)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let item:MoneyItem = appMoneyItems[indexPath.row]
        if moneys.contains(item.ex_title) {
            if  let index =  moneys.lastIndex(of: item.ex_title) {
                moneys.remove(at: index)
            }
        }else  {
            if moneys.count < 5 {
                moneys.append(item.ex_title)
            }
        }
        tableView.reloadData()
    
    }
    
    
}
